package com.betterjr.modules.sys.service;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import com.betterjr.common.service.BaseService;
import com.betterjr.common.utils.Collections3;
import com.betterjr.modules.sys.dao.DictItemInfoMapper;
import com.betterjr.modules.sys.entity.DictInfo;
import com.betterjr.modules.sys.entity.DictItemInfo;

@Service
public class DictItemService extends BaseService<DictItemInfoMapper, DictItemInfo> {

    @Autowired
    private DictService dictService;

    public List<DictItemInfo> findByGroup(int anParentId) {

        return this.selectByProperty("itemNo", anParentId);
    }

    public DictItemInfo saveDictItem(Integer anItemNo, String anKey, String anCode) {
        Map<String, Object> termMap = new HashMap();
        termMap.put("itemNo", anItemNo);
        termMap.put("itemValue", anKey);
        List<DictItemInfo> data = this.selectByProperty(termMap);
        DictItemInfo itemInfo = Collections3.getFirst(data);
        if (itemInfo == null) {
            itemInfo = new DictItemInfo(anKey, anCode, anItemNo);
            this.insert(itemInfo);
        } else {
            itemInfo.setItemCode(anCode);
            this.updateByPrimaryKey(itemInfo);
        }
        return itemInfo;
    }

    public List<DictItemInfo> findOutScriptByGroup(int anParentId) {
        Map<String, Object> map = new HashMap();
        map.put("itemNo", anParentId);
        map.put("status", Boolean.TRUE);
        map.put("exportOut", Boolean.TRUE);
        return this.selectByProperty(map);
    }

    /**
     * 
     * @Title: queryItemListByDictInfoName 
     * @Description: 根据字典表的编码查询对应的字典详情的所有信息
     * @param anCode
     * @return TODO(说明)
     * @throws 
     * @author xuyp
     * @date 2017年11月15日 上午10:16:11
     */
    public List<DictItemInfo> queryItemListByDictInfoName(String anCode) {

        List<DictItemInfo> list = new ArrayList<>();

        if (StringUtils.isNoneBlank(anCode)) {

            DictInfo dict = dictService.findByCode(anCode);

            if (null != dict) {
                list = this.findByGroup(dict.getId());
            }

        }

        return list;

    }
}
